﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XsqTech.SmartOA.Infrastructure.Filter
{
    public class GlobalActionFilter : IAsyncActionFilter
    {
        private readonly ILogger<GlobalExceptionFilter> _logger;

        public GlobalActionFilter(ILogger<GlobalExceptionFilter> logger)
        {
            _logger = logger;
        }
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();//开始计时
                       //要做的事情
            await next(); // 调用Action方法
            sw.Stop(); //停止计时

            long n = sw.ElapsedMilliseconds; //所用的毫秒数
                                           //还可以记录其他信息
                                           //略
                                           //记录Nlog日志
            _logger.LogInformation(n.ToString());
        }
    }
}
